<%--<%@ page import="com.java.model.Account" %>--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/layui/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>权限列表</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <script type="text/html" id="toolbarDemo"></script>

                    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                    <script type="text/html" id="currentTableBar"></script>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确定保存</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

    </div>
</div>
<script src="/layui/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table = $.extend(table, {config: {checkName: 'checked'}})

        table.render({
            elem: '#currentTableId',
            url: '/role/listPermission?roleId=${id}',
            toolbar: '#toolbarDemo',
            method: 'get',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', title: 'ID', sort: true},
                {field: 'permiName', title: '权限名称'},
                {field: 'permiUrl', title: '请求地址'},
                {field: 'updateTime', title: '更新时间'},
            ]],
            limits: [5, 10, 20, 30, 50, 100],
            limit: 10,
            page: true,
            request:{
                pageName:'pageNum',
                limitName:'pageSize',
            }
        });

        $('input[name=name]').change(function () {
           var name = $('#name').val();
           table.reload('currenTableId', {
               page: {
                   curr: 1
               }
               ,where: {
                   name: name
               }
           }, 'data');
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });

        form.on('submit(saveBtn)', function (data) {
            var checkStatus = table.checkStatus('currentTableId'),
                tableData = checkStatus.data;
                data = data.field;

            var permissionIds = new Array();
            $.each(tableData, function (index, element) {
                permissionIds.push(element.id);
            });

            $.ajax({
                url:'/rolePermission/save',
                type:'post',
                data: '{"roleId": ' + ${id} + ', "permissionIds": [' + permissionIds + ']}',
                contentType: 'application/json',
                dataType:'json',
                success:function (index){
                    if (index.code == '0'){
                        // 关闭弹出层
                        layer.close();
                        // 刷新上一层页面
                        parent.window.location.reload();
                    }
                }
            });
            return false;
        })
    });


    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

</script>

</body>
</html>
